package service.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.hash.HashMapper;
import service.statistics.client.UcenterClient;
import service.statistics.entity.Counter;
import service.statistics.entity.vo.CounterVo;
import service.statistics.mapper.CounterMapper;
import service.statistics.service.CounterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author diao
 * @since 2020-09-23
 */
@Service
public class CounterServiceImpl extends ServiceImpl<CounterMapper, Counter> implements CounterService {
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void countData(String date) {
        //调用接口获得某一天注册的会员数
        Integer registerMembers = ucenterClient.countRegisterMembers(date);
        //创建统计实体类
        Counter counter = new Counter();
        Integer loginNum = RandomUtils.nextInt(100, 200);
        Integer videoViewNum = RandomUtils.nextInt(100, 200);
        Integer courseNum = RandomUtils.nextInt(100, 200);
        //判断该日期是否已存在统计数据库中
        QueryWrapper<Counter> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", date);
        //给实体类赋值属性
        counter.setRegisterNum(registerMembers);
        counter.setLoginNum(loginNum);
        counter.setVideoViewNum(videoViewNum);
        counter.setCourseNum(courseNum);
        counter.setDateCalculated(date);
        counter.setGmtCreate(new Date());
        counter.setGmtModified(new Date());
        //如果有重复日期则执行更新
        int i = baseMapper.update(counter, wrapper);
        //无重复日期则新增统计数据
        if (i==0){
            baseMapper.insert(counter);
        }
    }

    //根据条件返回统计数据
    @Override
    public HashMap<String, Object> getData(CounterVo countervo) {
        /*
           因为json对象对应后端的map集合或
           而ECharts需要的json数组则对应后端的list集合
         */
        //需要设置两个list,分别用来存储时间和统计数大小
        List<String> dateList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        //获得前端条件
        String dataType = countervo.getType();
        String begin = countervo.getBegin();
        String end = countervo.getEnd();
        //条件查询
        QueryWrapper<Counter> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end );
        wrapper.select("date_calculated",dataType);//只查询出该两个字段,其中dataType对应某种统计分类
        List<Counter> counterList = baseMapper.selectList(wrapper);
        //遍历统计数据列表
        for (Counter counter : counterList) {
            String dateCalculated = counter.getDateCalculated();
            dateList.add(dateCalculated);
            //根据switch来判断统计类型
            switch (dataType) {
                case "register_num":
                    countList.add(counter.getRegisterNum());
                    break;
                case "login_num":
                    countList.add(counter.getLoginNum());
                    break;
                case "video_view_num":
                    countList.add(counter.getVideoViewNum());
                    break;
                case "course_num":
                    countList.add(counter.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        //建立map来存储两个json数组即日期list和统计量list
        HashMap<String, Object> map = new HashMap<>();
        map.put("dateList", dateList);
        map.put("countList", countList);
        //返回map
        return map;
     }
}
